package com.kurumi.autoprint.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.kurumi.autoprint.bo.Page;
import com.kurumi.autoprint.bo.PageData;
import com.kurumi.autoprint.bo.PageParam;
import com.kurumi.autoprint.service.order.PrintMrDetailService;
import com.kurumi.autoprint.util.ExcelUtil;
import com.kurumi.autoprint.util.FileUtil;
import com.kurumi.autoprint.util.JsonUtil;

@Controller
@RequestMapping("/search")
public class PrintSearchController {

	@Autowired
	private PrintMrDetailService printMrDetailService;

	/**
	 * 跳转到用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/toPrintDetailListPage")
	public ModelAndView toAdminUserListPage() throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();

		String ipJson = FileUtil.readResourceFile("payUmsTidBindIp.json ");
		List<Map> list = JsonUtil.jsonToList(ipJson, Map.class);
		model.put("IPLIST", list);

		System.out.println(list);

		return new ModelAndView("/search/printDetailListPage", model);
	}

	@ResponseBody
	@RequestMapping(value = "/getPrintDetailList")
	public void getPrintDetailList(HttpServletRequest request, HttpServletResponse response, PageParam pageParam)
			throws Exception {
		Page pager = new Page();
		pager.setPage(pageParam.getPage());
		pager.setRecPerPage(pageParam.getLimit());

		PageData<Map<String, Object>> pd = new PageData<Map<String, Object>>();
		PageInfo<Map<String, Object>> pageInfo = printMrDetailService.getPrintDetailPageInfo(request, pageParam);

		List<Map<String, Object>> list = pageInfo.getList();

		System.out.println(list);
		if (list != null && list.size() > 0) {
			pd.setCount((int) pageInfo.getTotal());
			pd.setData(list);
		} else {
			pd.setCount(0);
			pd.setData(new ArrayList<Map<String, Object>>());
		}

		String json = JsonUtil.objectToJson(pd);

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try {
			out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 跳转到用户信息
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/printDetailPage")
	public ModelAndView printDetailPage(Integer orderId) throws Exception {
		Map<String, Object> model = new HashMap<String, Object>();

		Map<String, Object> printDetailMap = printMrDetailService.getPrintDetailByOrderId(orderId);
		model.put("printDetail", printDetailMap);
		model.put("view", 1);

		return new ModelAndView("/search/printDetailPage", model);
	}

	
	
	@RequestMapping("/exportPrintDetail")
	public void exportPrintDetail(HttpServletResponse response,HttpServletRequest request) {
		response.setContentType("octets/stream");
		String excelName ="住院病历自助打印详情";
		String [] header = {"姓名","身份证号","出院科室","出院时间","住院次数","联系电话","自助机","收费途径","商户号","订单号","费用(元)","支付时间","打印类型","单价(元)","打印总页数","打印状态","异常信息"};
		// 转码防止乱码
		try {
			response.addHeader("Content-Disposition",
					"attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xls");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		// 通过response获取文件输出流
		OutputStream out = null;
		try {
			out = response.getOutputStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		// 查询数据
		List<Map<String,Object>> list = printMrDetailService.getPrintDetailInfoByParams(request);
		
		
		ExcelUtil.exportExcel(out, header,list);
		
		

	}

}
